var myTool = (function(win, doc){
    /* 
    获取非行间样式函数封装
    @params  el:element   attr: string
    */
    function getStyle(el, attr){
        return win.getComputedStyle ? getComputedStyle(el)[attr] : el.currentStyle[attr];
    }

    /* 
        匀速运动函数封装
        @params el:element  attr:string  taget:number  step:number
    */
    var timer = null;

    function linearMove(el, attr, target, step){
        clearInterval(timer);
        step = target > parseInt(getStyle(el, attr)) ? step : -step;
        timer = setInterval(function(){
            var cur = parseInt(getStyle(el, attr));
            var speed = cur + step;
            if(step > 0 && speed >= target || step < 0 && speed <= target){
                speed = target;
                clearInterval(timer);
            }
            el.style[attr] = speed + 'px';
        }, 20)
    }

    /* 
        创建元素的函数封装
        @params：tagName: string 标签名  content: string 标签的内容
    */
    function cEl(tagName, content){
        content = content || '';
        var el = doc.createElement(tagName);
        el.innerHTML = content;
        return el;
    }

    /* 
        获取元素到文档顶部的距离函数封装
        @params： el:element
    */
    function getTop(el){
        var iTop = 0;
        while(el.offsetParent){
            iTop += el.offsetTop;
            el = el.offsetParent;
        }
        return iTop;
    }

    /* 
        事件绑定函数封装
        @params： el:element   oEvent: string   fn: function
    */
    function bindEvent(el, oEvent, fn){
        el.addEventListener ? el.addEventListener(oEvent, fn, false) : el.attachEvent('on' + oEvent, fn);
    }

    /* 
        事件取消绑定函数封装
        @params： el:element   oEvent: string   fn: function
    */
    function removeEvent(el, oEvent, fn){
        el.removeEventListener ? el.removeEventListener(oEvent, fn, false) : el.detachEvent('on' + oEvent, fn);
    }

    /* 
        缓冲运动函数封装
        @params  el:element   obj:object  fn:function 
    */
    function bufferMove(el, obj, fn){
        clearInterval(el.timer);
        el.timer = setInterval(function(){
            var flag = true;
            for(var attr in obj){
                if(attr == 'opacity'){
                    var cur = Math.round(getStyle(el, attr) * 100);
                }else{
                    var cur = parseInt(getStyle(el, attr));
                }
                
                var step = (obj[attr] - cur) / 10;
                step = step > 0 ? Math.ceil(step) : Math.floor(step);
                if(cur != obj[attr]){
                    flag = false;
                }
        
                if(attr == 'opacity'){
                    el.style.opacity = (cur + step) / 100;
                    el.style.filter = 'alpha(opacity = ' + (cur + step) + ')'
                }else{
                    el.style[attr] = cur + step + 'px';
                }
            }

            
            if(flag){
                clearInterval(el.timer);
                // if(fn) fn();
                if(fn) fn.call(el);//fn要执行 但是不让window执行  喊call el过来执行fn 那么fn的this就指向 el
            }
        },20)
    }

    /* 
        随机n位验证码
    */
    function checkCode(n){
        var str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        var code = '';//最终的验证码
        // 遍历str n次 每次随机获取一位字符 累加到code
        for(var i = 0; i < n; i++){
            var start = Math.floor(Math.random() * str.length);
            code += str.substr(start, 1);
        }

        return code;
    }

    return {
        'getStyle' : getStyle,
        'linearMove' : linearMove,
        'cEl': cEl,
        'getTop' : getTop,
        'bindEvent' : bindEvent,
        'removeEvent': removeEvent,
        'bufferMove': bufferMove,
        'checkCode' : checkCode
    }
})(window, document)



// var myTool = {
//     'getStyle' : getStyle,
//     'linearMove' : linearMove,
//     'cEl': cEl,
//     'getTop' : getTop,
//     'bindEvent' : bindEvent,
//     'removeEvent': removeEvent,
//     'bufferMove': bufferMove
// }

// myTool.getStyle(....)
// myTool.bufferMove()


